\usepackage[compact]{titlesec}
\usepackage[framemethod=TikZ]{mdframed}
\titlespacing*{\subsection}{0pt}{0pt}{0pt}
\usepackage{tikz,mdframed}

\newcounter{theo}[section] 
\newenvironment{exercice}[1][]{%
\stepcounter{theo}% 
    \ifstrempty{#1}% 
    {\mdfsetup{%
        frametitle={%
            \tikz[baseline=(current bounding box.east),outer sep=0pt]
            \node[anchor=east,rectangle,fill=white]
            {\strut\thetheo};}}%
    }%
    {\mdfsetup{% 
        frametitle={%
            \tikz[baseline=(current bounding box.east),outer sep=0pt]
            \node[anchor=east,rectangle,fill=white]
            {\strut\thetheo~ #1};}}%
    }%
    \mdfsetup{innertopmargin=10pt,linecolor=black,%
            linewidth=1pt,topline=true,
            frametitleaboveskip=\dimexpr-\ht\strutbox\relax,}
    \begin{mdframed}[]\relax\vspace{-15px}%
    }{\end{mdframed}}


